﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using Samurai.Client.Commands;
using Samurai.Infrastructure;
using System;

namespace Samurai.Client.DataContexts
{
    public class MoviesDataContext : ADataContext, INotifyPropertyChanged
    {
        private string _abcd;
        public string Abcd
        {
            get { return _abcd; }
            set
            {
                _abcd = value;
                OnAbcdPropertyChanged();
            }
        }

        private void OnAbcdPropertyChanged()
        {
            if(PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Abcd"));
            ShowByGenreCommand.Execute(_abcd);

        }

        private static MoviesDataContext _instance;
        public static MoviesDataContext Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new MoviesDataContext();
                return _instance;
            }
        }

        public OpenDirectoryCommand OpenDirectoryCommand { get; set; }
        public RefreshMoviesCommand RefreshMoviesCommand { get; set; }

        public ShowByGenreCommand ShowByGenreCommand { get; set; }

        public SearchCommand SearchCommand { get; set; }

        public GotoUrlCommand GotoUrlCommand { get; set; }

        public string[] GroupingCategories { get; set; }

        private string _selectedGroupingCategory;
        public string SelectedGroupingCategory
        {
            get
            {
                return _selectedGroupingCategory;
            }
            set
            {
                _selectedGroupingCategory = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedGroupingCategory"));
            }
        }

        private MoviesDataContext()
        {
            Genres = new ObservableCollection<string>(new[] { "All" });

            Movies = new ObservableCollection<Movie>();

            OpenDirectoryCommand = OpenDirectoryCommand.Instance;
            RefreshMoviesCommand = new RefreshMoviesCommand(Movies, Genres);

            GotoUrlCommand = new GotoUrlCommand();

            ShowByGenreCommand = new ShowByGenreCommand(Movies);
            
            SearchCommand = new SearchCommand(Movies);

            CreateMovieCommand.Instance.MovieCreated += Instance_MovieCreated;

        }

        void Instance_MovieCreated(object sender, System.EventArgs e)
        {
            RefreshMoviesCommand.Execute(null);
        }

        public ObservableCollection<string> Genres { get; set; }

        public ObservableCollection<Movie> Movies { get; set; }

        public ObservableCollection<GroupedMovie> GroupedMovies { get; set; }

        public override void OnLoaded()
        {
            RefreshMoviesCommand.Execute(null);
            Abcd = "All";           

        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
